package com.smart.core;

import java.awt.Component;
import java.awt.Container;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.jdom2.Document;
import org.jdom2.Element;

import com.smart.gui.About_Frame;
import com.smart.gui.Business_Drivers_Part1;
import com.smart.gui.Business_Drivers_Part2;
import com.smart.gui.Business_Drivers_Part3;
import com.smart.gui.Define_Candidate_Test_Cases_Part1;
import com.smart.gui.Define_Candidate_Test_Cases_Part2;
import com.smart.gui.Define_Candidate_Test_Cases_Part3;
import com.smart.gui.Describe_Existing_Test_Environment_Part1;
import com.smart.gui.Describe_Existing_Test_Environment_Part2;
import com.smart.gui.Describe_Existing_Test_Environment_Part3;
import com.smart.gui.Describe_Existing_Test_Environment_Part4;
import com.smart.gui.Describe_Existing_Test_Environment_Part5;
import com.smart.gui.Describe_Existing_Test_Environment_Part6;
import com.smart.gui.Describe_Existing_Test_Environment_Part7;
import com.smart.gui.Describe_Target_Test_Environment_Part1;
import com.smart.gui.Describe_Target_Test_Environment_Part2;
import com.smart.gui.Describe_Target_Test_Environment_Part3;
import com.smart.gui.Describe_Target_Test_Environment_Part4;
import com.smart.gui.Legacy_and_Target_Test_Environment_Part1;
import com.smart.gui.Legacy_and_Target_Test_Environment_Part2;
import com.smart.gui.Legacy_and_Target_Test_Environment_Part3;
import com.smart.gui.SMART_T_Main_Application_Window;
import com.smart.gui.SMART_T_Questionnaire_Form;
import com.smart.gui.Stakeholders_Part1;
import com.smart.gui.Stakeholders_Part2;
import com.smart.gui.Stakeholders_Part3;
import com.smart.gui.Stakeholders_Part4;
import com.smart.gui.Stakeholders_Part5;
import com.smart.gui.TOC_Form_Business_Drivers;
import com.smart.gui.TOC_Form_Define_Candidate_Test_Cases;
import com.smart.gui.TOC_Form_Describe_Existing_Test_Environment;
import com.smart.gui.TOC_Form_Describe_Target_Test_Environment;
import com.smart.gui.TOC_Form_Legacy_and_Target_Test_Environment;
import com.smart.gui.TOC_Form_Operational_Results;
import com.smart.gui.TOC_Form_Stakeholders;
import com.smart.gui.TOC_Form_Technical_Factors;

// GUI Classes

public class AppManager {
	private FileMaker fm;
	// private GUI
	private About_Frame af;
	private Business_Drivers_Part1 bdp1;
	private Business_Drivers_Part2 bdp2;
	private Business_Drivers_Part3 bdp3;
	private Define_Candidate_Test_Cases_Part1 dctcp1;
	private Define_Candidate_Test_Cases_Part2 dctcp2;
	private Define_Candidate_Test_Cases_Part3 dctcp3;
	private Describe_Existing_Test_Environment_Part1 detep1;
	private Describe_Existing_Test_Environment_Part2 detep2;
	private Describe_Existing_Test_Environment_Part3 detep3;
	private Describe_Existing_Test_Environment_Part4 detep4;
	private Describe_Existing_Test_Environment_Part5 detep5;
	private Describe_Existing_Test_Environment_Part6 detep6;
	private Describe_Existing_Test_Environment_Part7 detep7;
	private Describe_Target_Test_Environment_Part1 dttep1;
	private Describe_Target_Test_Environment_Part2 dttep2;
	private Describe_Target_Test_Environment_Part3 dttep3;
	private Describe_Target_Test_Environment_Part4 dttep4;
	private Legacy_and_Target_Test_Environment_Part1 lattep1;
	private Legacy_and_Target_Test_Environment_Part2 lattep2;
	private Legacy_and_Target_Test_Environment_Part3 lattep3;
	private SMART_T_Main_Application_Window stmaw;
	private SMART_T_Questionnaire_Form stqf;
	private Stakeholders_Part1 sp1;
	private Stakeholders_Part2 sp2;
	private Stakeholders_Part3 sp3;
	private Stakeholders_Part4 sp4;
	private Stakeholders_Part5 sp5;
	private TOC_Form_Business_Drivers toc_f_bd;
	private TOC_Form_Define_Candidate_Test_Cases toc_f_dctc;
	private TOC_Form_Describe_Existing_Test_Environment toc_f_dete;
	private TOC_Form_Describe_Target_Test_Environment toc_f_dtte;
	private TOC_Form_Legacy_and_Target_Test_Environment toc_f_latte;
	private TOC_Form_Operational_Results toc_f_or;
	private TOC_Form_Stakeholders toc_f_s;
	private TOC_Form_Technical_Factors toc_f_tf;

	public Document doc;
	public File fi;

	// private static float tpct;
	private static float tyes;
	private static float tno;
	private static boolean tresult;
	private static float tpct;

	/**
	 * singleton pattern -- getInstance
	 */
	private static AppManager uniqueInstanceForm;

	public static synchronized AppManager getInstance() {
		if (uniqueInstanceForm == null) {
			try {
				uniqueInstanceForm = new AppManager();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return uniqueInstanceForm;
	}

	public AppManager() {
		fm = new FileMaker();
		fi = fm.getFile("SMART.xml");
		doc = fm.getDocument(fi, "SMART_APP");

		// get instance and set visible all GUI frames
		af = About_Frame.getInstance();
		bdp1 = Business_Drivers_Part1.getInstance();
		bdp2 = Business_Drivers_Part2.getInstance();
		bdp3 = Business_Drivers_Part3.getInstance();
		dctcp1 = Define_Candidate_Test_Cases_Part1.getInstance();
		dctcp2 = Define_Candidate_Test_Cases_Part2.getInstance();
		dctcp3 = Define_Candidate_Test_Cases_Part3.getInstance();
		detep1 = Describe_Existing_Test_Environment_Part1.getInstance();
		detep2 = Describe_Existing_Test_Environment_Part2.getInstance();
		detep3 = Describe_Existing_Test_Environment_Part3.getInstance();
		detep4 = Describe_Existing_Test_Environment_Part4.getInstance();
		detep5 = Describe_Existing_Test_Environment_Part5.getInstance();
		detep6 = Describe_Existing_Test_Environment_Part6.getInstance();
		detep7 = Describe_Existing_Test_Environment_Part7.getInstance();
		dttep1 = Describe_Target_Test_Environment_Part1.getInstance();
		dttep2 = Describe_Target_Test_Environment_Part2.getInstance();
		dttep3 = Describe_Target_Test_Environment_Part3.getInstance();
		dttep4 = Describe_Target_Test_Environment_Part4.getInstance();
		lattep1 = Legacy_and_Target_Test_Environment_Part1.getInstance();
		lattep2 = Legacy_and_Target_Test_Environment_Part2.getInstance();
		lattep3 = Legacy_and_Target_Test_Environment_Part3.getInstance();
		stmaw = SMART_T_Main_Application_Window.getInstance();
		stqf = SMART_T_Questionnaire_Form.getInstance();
		sp1 = Stakeholders_Part1.getInstance();
		sp2 = Stakeholders_Part2.getInstance();
		sp3 = Stakeholders_Part3.getInstance();
		sp4 = Stakeholders_Part4.getInstance();
		sp5 = Stakeholders_Part5.getInstance();
		toc_f_bd = TOC_Form_Business_Drivers.getInstance();
		toc_f_dctc = TOC_Form_Define_Candidate_Test_Cases.getInstance();
		toc_f_dete = TOC_Form_Describe_Existing_Test_Environment.getInstance();
		toc_f_dtte = TOC_Form_Describe_Target_Test_Environment.getInstance();
		toc_f_latte = TOC_Form_Legacy_and_Target_Test_Environment.getInstance();
		toc_f_or = TOC_Form_Operational_Results.getInstance();
		toc_f_s = TOC_Form_Stakeholders.getInstance();
		toc_f_tf = TOC_Form_Technical_Factors.getInstance();
		
		



	}

	public enum DesiredEnum {
		UNK(""), YES("Yes"), NO("No"), MAYBE("Maybe"), IGNORE("Ignore");

		// ------------------------------ FIELDS ------------------------------

		/**
		 * human name of variety
		 */

		private final String name;

		// -------------------------- PUBLIC INSTANCE METHODS
		// --------------------------

		/**
		 * override standard toString
		 * 
		 * @return name of variety
		 */
		public String toString() {
			return name;
		}

		// --------------------------- CONSTRUCTORS ---------------------------

		/**
		 * constructor
		 * 
		 * @param name
		 *            human name of variety
		 */
		DesiredEnum(String name) {
			this.name = name;
		}

	}

	public ArrayList<JFrame> getList() {
		ArrayList<JFrame> guiList = new ArrayList<JFrame>();

		// about frame
		guiList.add(af);

		// main app window
		guiList.add(stmaw);

		// smart questionaire form
		guiList.add(stqf);

		// business drivers
		guiList.add(toc_f_bd);
		guiList.add(bdp1);
		guiList.add(bdp2);
		guiList.add(bdp3);

		// technical factors
		guiList.add(toc_f_tf);
		guiList.add(toc_f_dctc);
		guiList.add(toc_f_dete);
		guiList.add(toc_f_dtte);
		guiList.add(toc_f_latte);
		guiList.add(dctcp1);
		guiList.add(dctcp2);
		guiList.add(dctcp3);
		guiList.add(detep1);
		guiList.add(detep2);
		guiList.add(detep3);
		guiList.add(detep4);
		guiList.add(detep5);
		guiList.add(detep6);
		guiList.add(detep7);
		guiList.add(dttep1);
		guiList.add(dttep2);
		guiList.add(dttep3);
		guiList.add(dttep4);
		guiList.add(lattep1);
		guiList.add(lattep2);
		guiList.add(lattep3);

		// operational results
		guiList.add(toc_f_or);
		guiList.add(toc_f_s);
		guiList.add(sp1);
		guiList.add(sp2);
		guiList.add(sp3);
		guiList.add(sp4);
		guiList.add(sp5);

		return guiList;

	}

	public ArrayList<JFrame> getBDList() {
		float pct = 0.0f;
		ArrayList<JFrame> guiList = new ArrayList<JFrame>();
		// guiList.add(toc_f_bd);
		guiList.add(bdp1);
		guiList.add(bdp2);
		guiList.add(bdp3);

		return guiList;
	}

	public ArrayList<JFrame> getTDList() {
		ArrayList<JFrame> guiList = new ArrayList<JFrame>();
		guiList.add(dctcp1);
		guiList.add(dctcp2);
		guiList.add(dctcp3);
		guiList.add(detep1);
		guiList.add(detep2);
		guiList.add(detep3);
		guiList.add(detep4);
		guiList.add(detep5);
		guiList.add(detep6);
		guiList.add(detep7);
		guiList.add(dttep1);
		guiList.add(dttep2);
		guiList.add(dttep3);
		guiList.add(dttep4);
		guiList.add(lattep1);
		guiList.add(lattep2);
		guiList.add(lattep3);
		return guiList;
	}

	public ArrayList<JFrame> getORList() {
		ArrayList<JFrame> guiList = new ArrayList<JFrame>();
		guiList.add(sp1);
		guiList.add(sp2);
		guiList.add(sp3);
		guiList.add(sp4);
		guiList.add(sp5);
		return guiList;
	}

	public void runApp() {
		// System.out.println("Starting SMART-T Main Application Window");
		stmaw.setVisible(true);
		setAllComps();
		updateStatus();
		getAllComps(stmaw, stmaw);
	}

	public void saveXML() {
		// System.out.println("about to output xml");
		fm.outputXML(doc, fi);
	}

	public boolean setText(Element elem, String data, String txt) {
		if (fm.writeToFile(doc, elem, data, txt)) {
			// System.out.println("set text successful");
			saveXML();
			return true;
		} else
			return false;
	}

	public void populateGUI() {
	}

	public String getText(String elemName) {
		String retval = null;
		retval = fm.getText(doc, elemName);
		return retval;
	}

	public static float calcPctComplete(float scb, float ecb) {
		if (!((ecb + scb) == 0)) {
			tpct = ((scb / (ecb + scb)) * 100);
		} else {
			tpct = 0;
			// System.out.println("Percent Complete: 0n");
		}
		return tpct;
	}

	public float getPctComplete(List<JFrame> guiList) {
		float scb = 0;
		float ecb = 0;
		float pct = 0;
		JFrame frame = new JFrame();
		Iterator<JFrame> i = guiList.iterator();
		while (i.hasNext()) {
			frame = (JFrame) i.next();
			for (Component c : frame.getContentPane().getComponents()) {
				String n = c.getClass().toString();

				if (c instanceof JComboBox<?>) {
					if (((JComboBox<?>) c).getModel().getSelectedItem()
							.equals(DesiredEnum.UNK)) {
						ecb++;
					} else {
						scb++;
						if (((JComboBox<?>) c).getModel().getSelectedItem()
								.equals(DesiredEnum.YES)) {
							tyes++;
						}
						if (((JComboBox<?>) c).getModel().getSelectedItem()
								.equals(DesiredEnum.MAYBE)) {
							tyes++;
						}
						if (((JComboBox<?>) c).getModel().getSelectedItem()
								.equals(DesiredEnum.IGNORE)) {
							tyes++;
						}
						if (((JComboBox<?>) c).getModel().getSelectedItem()
								.equals(DesiredEnum.NO)) {
							tno++;
						}
						pct = calcPctComplete(scb, ecb);
					}
				}
			}
		}
		return pct;
	}

	public void shouldProceed() {
		float tpct = 0;
		tpct = this.getPctComplete(getList());
		if (tpct >= 100.0f) {
			if (tyes >= tno) {
				stqf.showAnswer("Proceed with the Migration.");
			} 
			else {
				stqf.showAnswer("Do not proceed with the Migration. Or do more analysis?");
			}
		} 
		else {
			stqf.showAnswer("Not ready to provide answer!");
		}
	}

	public void setAllComps() {
		ArrayList<JFrame> guiList = getList();
		JFrame frame = new JFrame();
		Iterator<JFrame> i = guiList.iterator();
		while (i.hasNext()) {
			frame = (JFrame) i.next();
			for (Component c : frame.getContentPane().getComponents()) {
				String n = c.getClass().toString();

				if (c instanceof JComboBox<?>) {

					String fdata = getText(frame.getClass().getName()
							.replace(".", "_")
							+ "_" + ((JComboBox<?>) c).getName());
					if (fdata != null) {
						if (fdata.equals("Yes")) {
							((JComboBox<?>) c).setSelectedItem(DesiredEnum.YES);
						}
						if (fdata.equals("No")) {
							((JComboBox<?>) c).setSelectedItem(DesiredEnum.NO);
						}
						if (fdata.equals("Maybe")) {
							((JComboBox<?>) c)
									.setSelectedItem(DesiredEnum.MAYBE);
						}
						if (fdata.equals("Ignore")) {
							((JComboBox<?>) c)
									.setSelectedItem(DesiredEnum.IGNORE);
						}
						// System.out.println("combo data " + fdata);
					}
				}

				else if (c instanceof JScrollPane) {
					JTextArea ta = (JTextArea) ((JScrollPane) c).getViewport()
							.getView();
					String fdata = getText(frame.getClass().getName()
							.replace(".", "_")
							+ "_" + ta.getName());
					ta.setText(fdata);
				}

				else if (c instanceof JCheckBox
						&& frame.getTitle().equals(
								"SMART-T Main Application Window")) {
					JCheckBox jcb = (JCheckBox) c;
					float bd1 = this.getPctComplete(this.getBDList());

					if (jcb.getName().equals("bd")) {
						if (bd1 > 99.9) {
							jcb.setSelected(true);
						} else {
							jcb.setSelected(false);
						}
					}

					float td1 = this.getPctComplete(this.getTDList());
					if (td1 > 99.9) {
						jcb.setSelected(true);
					} else {
						jcb.setSelected(false);
					}

					float or1 = this.getPctComplete(this.getORList());
					if (or1 > 99.9) {
						jcb.setSelected(true);
					} else {
						jcb.setSelected(false);
					}
				}
			}
		}
	}

	// public void getAllComps() {
	// ArrayList<JFrame> guiList = getList();
	// JFrame frame = new JFrame();
	// Iterator<JFrame> i = guiList.iterator();
	// while (i.hasNext()) {
	// frame = (JFrame) i.next();
	// for (Component c : frame.getComponents()) {
	// String n = c.getClass().toString();
	//
	// if (c instanceof JComboBox) {
	// JComboBox<?> jcb = (JComboBox<?>) c;
	// Element elem = new Element(frame.getClass().getName()
	// .replace(".", "_")
	// + "_" + jcb.getName());
	// String data = "Answer";
	// String txt = jcb.getModel().getSelectedItem().toString();
	// setText(elem, data, txt);
	// }
	//
	// else if (c instanceof JTextArea) {
	// JTextArea jta = (JTextArea) c;
	// Element elem = new Element(frame.getClass().getName()
	// .replace(".", "_")
	// + "_" + jta.getName());
	// String data = "Text";
	// String txt = jta.getText();
	// setText(elem, data, txt);
	// }
	//
	// else if (c instanceof JCheckBox
	// && frame.getTitle().equals(
	// "SMART-T Main Application Window")) {
	// System.out.println("found the main window");
	// JCheckBox jcb = (JCheckBox) c;
	// float bd1 = getPctComplete(getBDList());
	// System.out.println("bd cmplt: "+bd1);
	// if (jcb.getName().equals("bd")) {
	// if (bd1 > 99.9) {
	// System.out.println("bd cmplt");
	// jcb.setSelected(true);
	// } else {
	// jcb.setSelected(false);
	// }
	// }
	//
	// float td1 = getPctComplete(getTDList());
	// System.out.println("td cmplt: "+td1);
	// if (td1 > 99.9) {
	// System.out.println("td cmplt");
	// jcb.setSelected(true);
	// } else {
	// jcb.setSelected(false);
	// }
	//
	// float or1 = getPctComplete(getORList());
	// System.out.println("or cmplt: "+or1);
	// if (or1 > 99.9) {
	// System.out.println("or cmplt");
	// jcb.setSelected(true);
	// } else {
	// jcb.setSelected(false);
	// }
	// }
	// }
	// }
	// }

	public void updateStatus() {
		List guiList = getList();
		Iterator i = guiList.iterator();
		while (i.hasNext()) {
			getAllComps((JFrame) i.next(), (JFrame) i.next());
		}
	}

	public void getAllComps(Container parent, JFrame frame) {
		System.out.println("number of components: "
				+ parent.getComponentCount());
		for (Component c : parent.getComponents()) {
			String n = c.getClass().toString();
			System.out.println("Component_NAME: " + n);

			if (c instanceof JComboBox) {
				JComboBox<?> jcb = (JComboBox<?>) c;
				System.out.println(frame.getClass().getName().replace(".", "_")
						+ "_" + jcb.getName());
				Element elem = new Element(frame.getClass().getName()
						.replace(".", "_")
						+ "_" + jcb.getName());
				String data = "Answer";
				String txt = jcb.getModel().getSelectedItem().toString();
				System.out.println("@@!! get value: " + txt);
				setText(elem, data, txt);
			}

			else if (c instanceof JTextArea) {
				JTextArea jta = (JTextArea) c;
				System.out.println(frame.getClass().getName().replace(".", "_")
						+ "_" + jta.getName());
				Element elem = new Element(frame.getClass().getName()
						.replace(".", "_")
						+ "_" + jta.getName());
				String data = "Text";
				String txt = jta.getText();
				setText(elem, data, txt);
			}

			else if (c instanceof JCheckBox
					&& frame.getTitle().equals(
							"SMART-T Main Application Window")) {
				System.out
						.println("!!!!!!!!!!!found main window!!!!!!!!!!!!!!!!!");
				JCheckBox jcb = (JCheckBox) c;
				System.out.println("and the checkbox is: " + jcb.getName());
				float bd1 = getPctComplete(getBDList());
				System.out.println("bd1 %cmplt: " + bd1);

				if (jcb.getName().equals("bd")) {
					if (bd1 > 99.9) {
						System.out
								.println("@@@@!!!!!!!bd check box!!!!!!!!!!!!!!!!!");
						jcb.setSelected(true);
					} else {
						jcb.setSelected(false);
					}
				}

				float td1 = getPctComplete(getTDList());
				System.out.println("td1 %cmplt: " + td1);
				if (jcb.getName().equals("td")) {
					if (td1 > 99.9) {
						jcb.setSelected(true);
					} else {
						jcb.setSelected(false);
					}
				}

				float or1 = getPctComplete(getORList());
				System.out.println("or1 %cmplt: " + or1);
				if (jcb.getName().equals("or")) {
					if (or1 > 99.9) {
						jcb.setSelected(true);
					} else {
						jcb.setSelected(false);
					}
				}
			}

			if (c instanceof Container) {
				getAllComps((Container) c, frame);
			}
		}
	}

	public About_Frame get_af() {
		return af;
	}

	public Business_Drivers_Part1 get_bdp1() {
		return bdp1;
	}

	public Business_Drivers_Part2 get_bdp2() {
		return bdp2;
	}

	public Business_Drivers_Part3 get_bdp3() {
		return bdp3;
	}

	public Define_Candidate_Test_Cases_Part1 get_dctcp1() {
		return dctcp1;
	}

	public Define_Candidate_Test_Cases_Part2 get_dctcp2() {
		return dctcp2;
	}

	public Define_Candidate_Test_Cases_Part3 get_dctcp3() {
		return dctcp3;
	}

	public Describe_Existing_Test_Environment_Part1 get_detep1() {
		return detep1;
	}

	public Describe_Existing_Test_Environment_Part2 get_detep2() {
		return detep2;
	}

	public Describe_Existing_Test_Environment_Part3 get_detep3() {
		return detep3;
	}

	public Describe_Existing_Test_Environment_Part4 get_detep4() {
		return detep4;
	}

	public Describe_Existing_Test_Environment_Part5 get_detep5() {
		return detep5;
	}

	public Describe_Existing_Test_Environment_Part6 get_detep6() {
		return detep6;
	}

	public Describe_Existing_Test_Environment_Part7 get_detep7() {
		return detep7;
	}

	public Describe_Target_Test_Environment_Part1 get_dttep1() {
		return dttep1;
	}

	public Describe_Target_Test_Environment_Part2 get_dttep2() {
		return dttep2;
	}

	public Describe_Target_Test_Environment_Part3 get_dttep3() {
		return dttep3;
	}

	public Describe_Target_Test_Environment_Part4 get_dttep4() {
		return dttep4;
	}

	public Legacy_and_Target_Test_Environment_Part1 get_lattep1() {
		return lattep1;
	}

	public Legacy_and_Target_Test_Environment_Part2 get_lattep2() {
		return lattep2;
	}

	public Legacy_and_Target_Test_Environment_Part3 get_lattep3() {
		return lattep3;
	}

	public SMART_T_Main_Application_Window get_stmaw() {
		return stmaw;
	}

	public SMART_T_Questionnaire_Form get_stqf() {
		return stqf;
	}

	public Stakeholders_Part1 get_sp1() {
		return sp1;
	}

	public Stakeholders_Part2 get_sp2() {
		return sp2;
	}

	public Stakeholders_Part3 get_sp3() {
		return sp3;
	}

	public Stakeholders_Part4 get_sp4() {
		return sp4;
	}

	public Stakeholders_Part5 get_sp5() {
		return sp5;
	}

	public TOC_Form_Business_Drivers get_toc_f_bd() {
		return toc_f_bd;
	}

	public TOC_Form_Define_Candidate_Test_Cases get_toc_f_dctc() {
		return toc_f_dctc;
	}

	public TOC_Form_Describe_Existing_Test_Environment get_toc_f_dete() {
		return toc_f_dete;
	}

	public TOC_Form_Describe_Target_Test_Environment get_toc_f_dtte() {
		return toc_f_dtte;
	}

	public TOC_Form_Legacy_and_Target_Test_Environment get_toc_f_latte() {
		return toc_f_latte;
	}

	public TOC_Form_Operational_Results get_toc_f_or() {
		return toc_f_or;
	}

	public TOC_Form_Stakeholders get_toc_f_s() {
		return toc_f_s;
	}

	public TOC_Form_Technical_Factors get_toc_f_tf() {
		return toc_f_tf;
	}

}
